package org.sbbs.app.gen.webapp.action;

import org.sbbs.app.gen.model.DetailEntity;
import org.sbbs.base.searcher.SearchFilterBuilder;
import org.sbbs.base.webapp.action.BaseJqgridPagedAction;
import org.sbbs.app.gen.service.DetailEntityManager;
import org.sbbs.base.pager.IPager;
import org.sbbs.base.pager.impl.JqgridPagerImpl;
import org.sbbs.base.searcher.SearchFilter;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.sbbs.base.pagequery.hibernate.CriteriaUtil;
import org.sbbs.base.pagequery.hibernate.CustomCriteria;
import org.sbbs.base.searcher.SearchOperator;
public class DetailEntityPagedAction extends BaseJqgridPagedAction {
	private DetailEntityManager	detailEntityManager;
	public DetailEntityManager getDetailEntityManager() {
		return detailEntityManager;
	}

	public void setDetailEntityManager(DetailEntityManager detailEntityManager) {
		this.detailEntityManager = detailEntityManager;
	}
	private Long dictionaryId;

	public Long getDictionaryId() {
		return dictionaryId;
	}

	public void setDictionaryId(Long dictionaryId) {
		this.dictionaryId = dictionaryId;
	}
	@Override
	public Class getSearchEntityClazz() {
		return DetailEntity.class;
	}

	@Override
	public int getSearchFilterType() {
		return SearchFilterBuilder.PROPERTY_FILTER;
	}

	@Override
	public String gridPageList() {
        this.setSearchFilterType( SearchFilterBuilder.PROPERTY_FILTER );
        this.setCustomCriteria(new DetailEntityCustomCriteria(this.dictionaryId));
        try {
            IPager pager = new JqgridPagerImpl( getRequest() );
            SearchFilter sf = SearchFilterBuilder.buildSearchFilter( getRequest(), this.getSearchFilterType() );
            if ( this.getCustomCriteria() != null )
                pager = this.detailEntityManager.pagedQuery( this.getSearchEntityClazz(), pager, sf, this.getCustomCriteria() );
            else
                pager = this.detailEntityManager.pagedQuery( this.getSearchEntityClazz(), pager, sf );

            this.pageList = pager.getList();
            records = pager.getTotalNumberOfRows();
            total = pager.getTotalNumberOfPages();
            page = pager.getPageNumber();
        }
        catch ( Exception e ) {
            log.error( "detailEntityGridList Error", e );
        }
        return SUCCESS;
    }
}
class DetailEntityCustomCriteria implements CustomCriteria {
	private Long	parentId;

	DetailEntityCustomCriteria(Long parentId) {
		this.parentId = parentId;
	}

	@Override
	public Criteria getCustomCriteria(Class clazz, Session session) throws Exception {
		Criteria cr = session.createCriteria(clazz);
		Criterion crn = CriteriaUtil.createCriterion(clazz, "masterEntity.dictionaryId", SearchOperator.eq.getOpr(), parentId+"");
		return cr.add(crn);
	}

}
